#include "common.h"
#include <vmsys.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

time_t easy_time()
{
	time_t ut = 0;
	vm_time_t now;
	if (vm_get_time(&now) == 0)
	{

	ut = (now.year - 1970) * 365 * 24 * 60 * 60;
	ut += now.mon * 30 * 24 * 60 * 60;
	ut += now.day * 24 * 60 * 60;
	ut += now.hour * 60 * 60;
	ut += now.min * 60;
	ut += now.sec;
	
	}
	return ut;
}

time_t easy_mktime (struct tm *tm)
{
	time_t ut = 0;
	ut = (tm->tm_year - 70) * 365 * 24 * 60 * 60;
	ut += tm->tm_yday * 24 * 60 * 60;
	ut += tm->tm_hour * 60 * 60;
	ut += tm->tm_min * 60;
	ut += tm->tm_sec;
	return ut;
}


char *
exml_strdup(const char *s)
{
    char	*t;
	
    if (s == NULL)
        return (NULL);
	
    if ((t = (char *)vm_malloc(strlen(s) + 1)) == NULL)
        return (NULL);
	
    return (strcpy(t, s));
}


/** utility*/

void bzero(void *s, int len)
{
	memset(s, 0, len);
}

/*
 * Return a static formatted string.
 */
const char *s_sprintf (const char *fmt, ...)
{
    static char *buf = NULL;
    static int size = 0;
    va_list args;
    char *nbuf;
    int rc, nsize;

    if (!buf)
        buf = (char *)vm_calloc(1024);

    while (1)
    {
        buf[size - 2] = '\0';
        va_start (args, fmt);
        rc = vsprintf (buf, fmt, args);
        va_end (args);
        
        if (rc >= 0 && rc < size && !buf[size - 2])
            break;

        nsize = (rc > 0 ? rc + 5 : size * 2);
        nbuf = (char *)vm_malloc (nsize);
        if (!nbuf)
            break;
        vm_free (buf);
        buf = nbuf;
        size = nsize;
    }
    return buf;
}
